﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace mebc4.tablebased
{
    public class SplitMap
    {
        public static IEnumerable<string> Split(string config)
        {
            Console.WriteLine("split: {0}, thread: {1}", config, Thread.CurrentThread.GetHashCode());
            return config.Split(';');
        }

        public static void ToLower(IEnumerable<string> assignments, Action<string> loweredAssignments)
        {
            Console.WriteLine("tolower: {0}, thread: {1}", assignments.Count(), Thread.CurrentThread.GetHashCode());
            assignments.ToList()
                       .ForEach(a => loweredAssignments(a.ToLower()));
            loweredAssignments(null);
        }

        public static void Map(string assignment, Action<KeyValuePair<string, string>> kvPairs)
        {
            Console.WriteLine("map: {0}, thread: {1}", assignment, Thread.CurrentThread.GetHashCode());
            if (assignment != null)
            {
                var ap = assignment.Split('=');
                kvPairs(new KeyValuePair<string, string>(ap[0], ap[1]));
            }
            else
                kvPairs(new KeyValuePair<string, string>(null, null));
        }

    }


    public class Build
    {
        private Dictionary<string, string> dict = new Dictionary<string, string>();

        public void Process(KeyValuePair<string, string> kvp)
        {
            Console.WriteLine("build: {0}, thread: {1}", kvp.Key, Thread.CurrentThread.GetHashCode());
            if (kvp.Key != null)
            {
                dict.Add(kvp.Key, kvp.Value);
            }
            else
            {
                Result(dict);
                dict = new Dictionary<string, string>();
            }
        }

        public event Action<Dictionary<string, string>> Result;
    }
}
